package checkers.gui.dialogs;

import checkers.model.Player;
import checkers.model.Player.Color;
import checkers.common.Settings;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

/**
 * Class ServerGameDialog
 * Show create server dialog
 * allows to set server information
 * 
 * @author Cristian Tardivo
 */
public class ServerGameDialog extends javax.swing.JDialog {
    private static final ResourceBundle lang = ResourceBundle.getBundle("checkers/common/lang");
    private boolean result;
    
    /**
     * Create new ServerGameDialog
     * @param parent Parent Frame
     * @param modal Modal exclude
     */
    public ServerGameDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        // Set default Values
        name_field.setText(Settings.getNamePlayerA());
        if(Settings.getColorPlayerA().equals(Color.red)){
            red_but.setSelected(true);
            black_but.setSelected(false);
        } else {
            red_but.setSelected(false);
            black_but.setSelected(true);
        }
        port_field.setText(Settings.getPort()+"");
        this.getRootPane().setDefaultButton(accept_but);
        // Key ESCAPE action
        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action escapeAction = new AbstractAction(){public void actionPerformed(ActionEvent e){cancel_but.doClick();}}; 
        this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
        this.getRootPane().getActionMap().put("ESCAPE", escapeAction);
    }
    
    /**
     * Create Result
     * @return 
     */
    public boolean getResult(){
        return result;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        red_but = new javax.swing.JRadioButton();
        black_but = new javax.swing.JRadioButton();
        color_lbl = new javax.swing.JLabel();
        name_lbl = new javax.swing.JLabel();
        name_field = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        port_field = new javax.swing.JTextField();
        accept_but = new javax.swing.JButton();
        cancel_but = new javax.swing.JButton();

        buttonGroup1.add(red_but);
        buttonGroup1.add(black_but);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(lang.getString("START_SERVER")); // NOI18N
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(lang.getString("LOCAL_PLAYER"))); // NOI18N

        red_but.setText(lang.getString("RED")); // NOI18N

        black_but.setText(lang.getString("BLACK")); // NOI18N
        black_but.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                black_butActionPerformed(evt);
            }
        });

        color_lbl.setText(lang.getString("COLOR")); // NOI18N

        name_lbl.setText(lang.getString("NAME")); // NOI18N

        name_field.setText(lang.getString("PLAYER_A")); // NOI18N

        jLabel1.setText(lang.getString("PORT")); // NOI18N

        port_field.setText("6143");
        port_field.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(color_lbl)
                    .addComponent(name_lbl))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(name_field, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(red_but)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(black_but))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(port_field)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(name_lbl)
                    .addComponent(name_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(black_but)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(color_lbl)
                        .addComponent(red_but)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(port_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap())
        );

        accept_but.setText(lang.getString("START")); // NOI18N
        accept_but.setToolTipText("");
        accept_but.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                accept_butActionPerformed(evt);
            }
        });

        cancel_but.setText(lang.getString("CANCEL")); // NOI18N
        cancel_but.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_butActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(accept_but)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancel_but)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(accept_but)
                    .addComponent(cancel_but))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void black_butActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_black_butActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_black_butActionPerformed

    private void cancel_butActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel_butActionPerformed
        result = false;
        dispose();
    }//GEN-LAST:event_cancel_butActionPerformed

    private void accept_butActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_accept_butActionPerformed
        if(name_field.getText().isEmpty()){
            name_field.requestFocus();
            return;
        }
        Settings.setNamePlayerA(name_field.getText());
        Settings.setColorPlayerA(red_but.isSelected()?Color.red:Color.black);
        Settings.setTypePlayerA(Player.Type.local);
        Settings.setColorPlayerB(red_but.isSelected()?Color.black:Color.red);
        Settings.setTypePlayerB(Player.Type.remote);
        result = true;
        dispose();
    }//GEN-LAST:event_accept_butActionPerformed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton accept_but;
    private javax.swing.JRadioButton black_but;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cancel_but;
    private javax.swing.JLabel color_lbl;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField name_field;
    private javax.swing.JLabel name_lbl;
    private javax.swing.JTextField port_field;
    private javax.swing.JRadioButton red_but;
    // End of variables declaration//GEN-END:variables
}
